package org.csu.onlineedubackend.service.impl;

import org.csu.onlineedubackend.entity.Notification;
import org.csu.onlineedubackend.mapper.NotificationMapper;
import org.csu.onlineedubackend.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;

    @Override
    @Transactional
    public void addNotification(Notification notification) {
        notificationMapper.insert(notification);
    }

    @Override
    public List<Notification> getUserNotifications(Integer userId) {
        return notificationMapper.findByUserId(userId);
    }

    @Override
    @Transactional
    public void markAllAsRead(Integer userId) {
        notificationMapper.markAllAsRead(userId);
    }

    @Override
    @Transactional
    public void markAsRead(Long notificationId, Integer userId) {
        notificationMapper.markAsRead(notificationId, userId);
    }

    @Override
    @Transactional
    public void cleanOldNotifications(Integer userId) {
        notificationMapper.deleteOldNotifications(userId);
    }

    @Override
    @Transactional
    public void deleteNotificationsByType(Integer userId, String type, String targetName) {
        notificationMapper.deleteByTypeAndTarget(userId, type, targetName);
    }

    @Override
    @Transactional
    public void updateNotificationType(Long notificationId, String newType) {
        notificationMapper.updateType(notificationId, newType);
    }

    @Override
    @Transactional
    public void updateNotificationTypeByUserAndTarget(Integer userId, String oldType, String targetName, String newType) {
        notificationMapper.updateTypeByUserAndTarget(userId, oldType, targetName, newType);
    }
} 